from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    # playwright基础操作示例

    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("http://www.xxxx.net:8087/ords")
    page.locator("#F4550_P1_COMPANY").fill("esun")
    page.locator("#F4550_P1_COMPANY").press("Tab")
    page.locator("#F4550_P1_USERNAME").fill("admin")
    page.locator("#F4550_P1_USERNAME").press("Tab")
    page.locator("#F4550_P1_PASSWORD").fill("gss=lx")
    page.locator("#F4550_P1_PASSWORD").press("Enter")
    # page.get_by_role("link", name="App Builder").click()
    page.locator("a.a-ImageNav-link.app-builder").click()
    # page.locator(".a-AppCards-itemContainer", has_text="161").click()
    with page.expect_popup() as page1_info:
        # page.locator(".a-AppCards-itemContainer", has_text="161").locator("button.launch-aut").click()
        page.locator(".a-AppCards-itemContainer:has-text('161')").locator("button.launch-aut").click()
        # page.get_by_role("cell", name="测试应用 161  ").get_by_title("Run").click()
    page1 = page1_info.value
    page1.locator("#P9999_USERNAME").fill("admin")
    page1.locator("#P9999_USERNAME").press("Tab")
    page1.locator("#P9999_PASSWORD").fill("gss=lx")
    # page1.get_by_role("button", name="登录").click()
    # page1.locator(".t-Login-buttons > button", has_text="登录").click()
    page1.locator(".t-Login-buttons > button:has-text('登录')").click()
    page1.locator("#t_Button_navControl").click()
    # page1.get_by_label("主导航").click()
    page1.get_by_role("menuitem", name="样式测试").click()
    # print(page1.locator("div#table-wrap div.t-fht-tbody  table.a-IRR-table"))
    page1.locator("tr > td.a-IRR-linkCol.u-tC > a").first.click()
    page1.locator("button.ui-dialog-titlebar-close[title=关闭]").click()
    page1.get_by_role("button", name="创建").click()
    page1.locator("button.ui-dialog-titlebar-close[title=关闭]").first.click()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
